package com.aier.service.report.impl;

import com.aier.bean.foreign.ForeignBase;
import com.aier.bean.forwarder.ForwarderCode;
import com.aier.bean.forwarder.FreightForwarder;
import com.aier.bean.report.Quotation;
import com.aier.service.foreign.ForeignBaseService;
import com.aier.service.forwarder.FreightForwarderService;
import com.aier.service.report.QuotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 报价表service
 */
@Service
public class QuotationServiceImpl implements QuotationService {

    @Autowired
    private ForeignBaseService foreignBaseService;

    @Autowired
    private FreightForwarderService freightForwarderService;

    @Override
    public List<Quotation> buildQuotation() {
        List<ForeignBase> foreignBaseList = foreignBaseService.getAll();
        List<Quotation>quotationList = new ArrayList<>();
        foreignBaseList.stream().forEach(foreignBase -> {
            Quotation quotation = new Quotation(foreignBase);
            //计算单箱方数
            quotation.setSingleBoxVolumetric(
                    foreignBase.getBoxLength()
                            .multiply(foreignBase.getBoxWidth()
                                    .multiply(foreignBase.getBoxHeight()))
                            .divide(BigDecimal.valueOf(1000000))
                            .setScale(2, RoundingMode.HALF_UP)
            );
            //计算总方数
            quotation.setTotalVolumetric(foreignBase.getBoxLength()
                    .multiply(foreignBase.getBoxWidth()
                            .multiply(foreignBase.getBoxHeight())
                            .multiply(foreignBase.getBoxNum()))
                    .divide(BigDecimal.valueOf(1000000))
                    .setScale(2, RoundingMode.HALF_UP)
            );
            //计算总重量
            quotation.setTotalWeight(foreignBase.getWeight()
                    .multiply(foreignBase.getBoxNum())
                    .setScale(2, RoundingMode.HALF_UP)
            );
            System.out.println(quotation.toString());
            System.out.println(foreignBase.toString());
            //计算体积重
            quotation.setVolumetricWeight(foreignBase.getBoxLength()
                    .multiply(foreignBase.getBoxWidth()
                            .multiply(foreignBase.getBoxHeight())
                            .divide(BigDecimal.valueOf(6000),2, RoundingMode.HALF_UP))
                    .setScale(2, RoundingMode.HALF_UP));
            //计算申报单价
            quotation.setDeclaredUnitPrice(BigDecimal.valueOf(167)
                    .multiply(quotation.getTotalVolumetric())
                    .divide(foreignBase.getBoxNum()
                            .multiply(foreignBase.getPerBoxQty()),2, RoundingMode.HALF_UP)
                    .setScale(2, RoundingMode.HALF_UP));
            //税费
            quotation.setTariffs(
                    BigDecimal.valueOf(0.35)
                            .multiply(quotation.getBoxNum())
                            .multiply(quotation.getPerBoxQty())
                            .multiply(quotation.getDeclaredUnitPrice())
                            .setScale(2, RoundingMode.HALF_UP));
            ForwarderCode[] forwarderCodes = ForwarderCode.values();
            Arrays.stream(forwarderCodes).forEach(forwarderCode -> {
                //根据 仓库,产品型号，建单时间查询
                List<FreightForwarder> forwarderList = freightForwarderService.getFreightForwarderByFba(foreignBase.getFbaCode());
                if (forwarderList != null && forwarderList.size() > 0){
                    forwarderList.stream().forEach(freightForwarder -> {
                        switch (freightForwarder.getForwarderCode()){
                            case "F01":
                                quotation.setHengdaTaxInclusivePrice(freightForwarder.getTaxInclusivePrice());
                                quotation.setHengdaSelfTaxedPrice(freightForwarder.getSelfFaxedPrice());
                                quotation.setHengdaTransportChannel(freightForwarder.getTransportChannel());
                                //包税总价 包税价 * 总总量
                                if (quotation.getHengdaTaxInclusivePrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setHengdaTotalTaxInclusivePrice(
                                            quotation.getHengdaTaxInclusivePrice()
                                                    .multiply(quotation.getTotalWeight())
                                                    .setScale(2, RoundingMode.HALF_UP));
                                }
                                //自税总价 自税价 * 总方数
                                if (quotation.getHengdaSelfTaxedPrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setHengdaTotalSelfTaxedPrice(
                                            quotation.getHengdaSelfTaxedPrice()
                                                    .multiply(quotation.getTotalVolumetric())
                                                    .setScale(2, RoundingMode.HALF_UP));

                                    quotation.setHengdaTotalFee(
                                            quotation.getHengdaTotalSelfTaxedPrice()
                                                    .add(quotation.getTariffs()));
                                }
                                break;
                            case "F02":
                                quotation.setZhiyeTaxInclusivePrice(freightForwarder.getTaxInclusivePrice());
                                quotation.setZhiyeSelfTaxedPrice(freightForwarder.getSelfFaxedPrice());
                                quotation.setZhiyeTransportChannel(freightForwarder.getTransportChannel());
                                //包税总价 包税价 * 总总量
                                if (quotation.getZhiyeTaxInclusivePrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setZhiyeTotalTaxInclusivePrice(
                                            quotation.getZhiyeTaxInclusivePrice()
                                                    .multiply(quotation.getTotalWeight())
                                                    .setScale(2, RoundingMode.HALF_UP));
                                }
                                //自税总价 自税价 * 总方数
                                if (quotation.getZhiyeSelfTaxedPrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setZhiyeTotalSelfTaxedPrice(
                                            quotation.getZhiyeSelfTaxedPrice()
                                                    .multiply(quotation.getTotalVolumetric())
                                                    .setScale(2, RoundingMode.HALF_UP));

                                    quotation.setZhiyeTotalFee(
                                            quotation.getZhiyeTotalSelfTaxedPrice()
                                                    .add(quotation.getTariffs()));
                                }
                                break;
                            case "F03":
                                quotation.setNiukuTaxInclusivePrice(freightForwarder.getTaxInclusivePrice());
                                quotation.setNiukuSelfTaxedPrice(freightForwarder.getSelfFaxedPrice());
                                quotation.setNiukuTransportChannel(freightForwarder.getTransportChannel());
                                //包税总价 包税价 * 总总量
                                if (quotation.getNiukuTaxInclusivePrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setNiukuTotalTaxInclusivePrice(
                                            quotation.getNiukuTaxInclusivePrice()
                                                    .multiply(quotation.getTotalWeight())
                                                    .setScale(2, RoundingMode.HALF_UP));
                                }
                                //自税总价 自税价 * 总方数
                                if (quotation.getNiukuSelfTaxedPrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setNiukuTotalSelfTaxedPrice(
                                            quotation.getNiukuSelfTaxedPrice()
                                                    .multiply(quotation.getTotalVolumetric())
                                                    .setScale(2, RoundingMode.HALF_UP));

                                    quotation.setNiukuTotalFee(
                                            quotation.getNiukuTotalSelfTaxedPrice()
                                                    .add(quotation.getTariffs()));
                                }
                                break;
                            case "F04":
                                quotation.setMaiyiTaxInclusivePrice(freightForwarder.getTaxInclusivePrice());
                                quotation.setMaiyiSelfTaxedPrice(freightForwarder.getSelfFaxedPrice());
                                quotation.setMaiyiTransportChannel(freightForwarder.getTransportChannel());
                                //包税总价 包税价 * 总总量
                                if (quotation.getMaiyiTaxInclusivePrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setMaiyiTotalTaxInclusivePrice(
                                            quotation.getMaiyiTaxInclusivePrice()
                                                    .multiply(quotation.getTotalWeight())
                                                    .setScale(2, RoundingMode.HALF_UP));
                                }
                                //自税总价 自税价 * 总方数
                                if (quotation.getMaiyiSelfTaxedPrice().compareTo(BigDecimal.ZERO)>0){
                                    quotation.setMaiyiTotalSelfTaxedPrice(
                                            quotation.getMaiyiSelfTaxedPrice()
                                                    .multiply(quotation.getTotalVolumetric())
                                                    .setScale(2, RoundingMode.HALF_UP));

                                    quotation.setMaiyiTotalFee(
                                            quotation.getMaiyiTotalSelfTaxedPrice()
                                                    .add(quotation.getTariffs()));
                                }
                                break;
                            default:
                                break;

                        }
                    });
                }
            });
            quotationList.add(quotation);
        });
        return quotationList;
    }

    public void calculateCommon(){}
}
